package com.tensquare.articlecrawler.pipeline;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import util.HTMLUtil;
import util.IKUtil;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

/**
 * 文章保存文本类
 */
@Component
public class ArticleTxtPipeline implements Pipeline{
    @Value("${ai.savePath}")
    private String savePath;

    private String channelId;

    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        //1.获取标题和内容
        String title = (String)resultItems.get("title");
        String content = (String)resultItems.get("content");

        //2.把content的标签去掉
        content = HTMLUtil.delHTMLTag(content);

        //3.对标题和内容进行ik分词
        try {
            String result = IKUtil.split(title+" "+content," ");

            //4.把结果保存文本
            PrintWriter writer = new PrintWriter(new File(savePath+"/"+channelId+"/"+ UUID.randomUUID()+".txt"));
            writer.write(result);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
